package com.jingding.procurement.controller;

import com.jingding.procurement.enumeration.EntityEnum;
import com.jingding.procurement.pojo.entity.*;
import com.jingding.procurement.util.IO;
import com.jingding.procurement.util.TimeFormat;
import com.jingding.procurement.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("timing")
@Slf4j
public class TimingController {


    //修改定时任务设置
    @PostMapping("settings")
    public JsonResult settings(@RequestBody Entity entity) throws IOException, ClassNotFoundException {
        if (entity.getPort().equals(EntityEnum.PCE.getCode())) {
            ProcurementContractEntity procurementContractEntity = IO.procurementContractRead();
            if (!entity.getDisable().isEmpty()) {
                procurementContractEntity.setDisable(entity.getDisable());
            }
            try {
                if (!entity.getTaskTime().isEmpty()) {
                    procurementContractEntity.setTaskTime(entity.getTaskTime());
                }
                if (!entity.getFirstTime().isEmpty()) {
                    procurementContractEntity.setFirstTime(entity.getFirstTime());
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }

            if (!entity.getStartTime().isEmpty()) {
                procurementContractEntity.setStartTime(entity.getStartTime());
            }
            IO.procurementContractWrite(procurementContractEntity);
            log.debug("修改[采购合同]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok("[采购合同]定时修改完成");
        } else if (entity.getPort().equals(EntityEnum.POE.getCode())) {
            PurchaseOrdersEntity purchaseOrdersEntity = IO.purchaseOrdersRead();
            if (!entity.getDisable().isEmpty()) {
                purchaseOrdersEntity.setDisable(entity.getDisable());
            }
            try {
                if (!entity.getTaskTime().isEmpty()) {
                    purchaseOrdersEntity.setTaskTime(entity.getTaskTime());
                }
                if (!entity.getFirstTime().isEmpty()) {
                    purchaseOrdersEntity.setFirstTime(entity.getFirstTime());
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            if (!entity.getStartTime().isEmpty()) {
                purchaseOrdersEntity.setStartTime(entity.getStartTime());
            }
            IO.purchaseOrdersWrite(purchaseOrdersEntity);
            log.debug("修改[采购订单]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok("[采购订单]定时修改完成");
        } else if (entity.getPort().equals(EntityEnum.PPE.getCode())) {
            PurchasePoundsEntity purchasePoundsEntity = IO.purchasePoundsRead();
            if (!entity.getDisable().isEmpty()) {
                purchasePoundsEntity.setDisable(entity.getDisable());
            }
            try {
                if (!entity.getTaskTime().isEmpty()) {
                    purchasePoundsEntity.setTaskTime(entity.getTaskTime());
                }
                if (!entity.getFirstTime().isEmpty()) {
                    purchasePoundsEntity.setFirstTime(entity.getFirstTime());
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            if (!entity.getStartTime().isEmpty()) {
                purchasePoundsEntity.setStartTime(entity.getStartTime());
            }
            IO.purchasePoundsWrite(purchasePoundsEntity);
            log.debug("修改[销售出库]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok("[销售出库]定时修改完成");
        } else if (entity.getPort().equals(EntityEnum.SOE.getCode())) {
            SellOutEntity sellOutEntity = IO.sellOutRead();
            if (!entity.getDisable().isEmpty()) {
                sellOutEntity.setDisable(entity.getDisable());
            }
            try {
                if (!entity.getTaskTime().isEmpty()) {
                    sellOutEntity.setTaskTime(entity.getTaskTime());
                }
                if (!entity.getFirstTime().isEmpty()) {
                    sellOutEntity.setFirstTime(entity.getFirstTime());
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            if (!entity.getStartTime().isEmpty()) {
                sellOutEntity.setStartTime(entity.getStartTime());
            }
            IO.sellOutWrite(sellOutEntity);
            log.debug("修改[销售出库]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok("[销售出库]定时修改完成");
        } else if (entity.getPort().equals(EntityEnum.SISE.getCode())) {
            SupplierInspectionSheetEntity supplierInspectionSheetEntity = IO.supplierInspectionSheetRead();
            if (!entity.getDisable().isEmpty()) {
                supplierInspectionSheetEntity.setDisable(entity.getDisable());
            }
            try {
                if (!entity.getTaskTime().isEmpty()) {
                    supplierInspectionSheetEntity.setTaskTime(entity.getTaskTime());
                }
                if (!entity.getFirstTime().isEmpty()) {
                    supplierInspectionSheetEntity.setFirstTime(entity.getFirstTime());
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            if (!entity.getStartTime().isEmpty()) {
                supplierInspectionSheetEntity.setStartTime(entity.getStartTime());
            }
            IO.supplierInspectionSheetWrite(supplierInspectionSheetEntity);
            log.debug("修改[供方检验单]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok("[供方检验单]定时修改完成");
        } else if (entity.getPort().equals(EntityEnum.TE.getCode())) {
            TonnageEntity tonnageEntity = IO.tonnageRead();
            if (!entity.getDisable().isEmpty()) {
                tonnageEntity.setDisable(entity.getDisable());
            }
            try {
                if (!entity.getTaskTime().isEmpty()) {
                    tonnageEntity.setTaskTime(entity.getTaskTime());
                }
                if (!entity.getFirstTime().isEmpty()) {
                    tonnageEntity.setFirstTime(entity.getFirstTime());
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            if (!entity.getStartTime().isEmpty()) {
                tonnageEntity.setStartTime(entity.getStartTime());
            }
            IO.tonnageWrite(tonnageEntity);
            log.debug("修改[扣吨计算单]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok("[扣吨计算单]定时修改完成");
        }
        return JsonResult.ok("定时修改失败,请检查服务参数!");
    }

    //查看定时任务设置
    @GetMapping("read")
    public JsonResult readings(String port) throws IOException, ClassNotFoundException {
        if (port.equals(EntityEnum.PCE.getCode())) {
            ProcurementContractEntity procurementContractEntity = IO.procurementContractRead();
            log.debug("查询[采购合同]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok(procurementContractEntity);
        } else if (port.equals(EntityEnum.POE.getCode())) {
            PurchaseOrdersEntity purchaseOrdersEntity = IO.purchaseOrdersRead();
            log.debug("查询[采购订单]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok(purchaseOrdersEntity);
        } else if (port.equals(EntityEnum.PPE.getCode())) {
            PurchasePoundsEntity purchasePoundsEntity = IO.purchasePoundsRead();
            log.debug("查询[采购磅单]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok(purchasePoundsEntity);
        } else if (port.equals(EntityEnum.SOE.getCode())) {
            SellOutEntity sellOutEntity = IO.sellOutRead();
            log.debug("查询[销售出库]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok(sellOutEntity);
        } else if (port.equals(EntityEnum.SISE.getCode())) {
            SupplierInspectionSheetEntity supplierInspectionSheetEntity = IO.supplierInspectionSheetRead();
            log.debug("查询[供方检验单]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok(supplierInspectionSheetEntity);
        } else if (port.equals(EntityEnum.TE.getCode())) {
            TonnageEntity tonnageEntity = IO.tonnageRead();
            log.debug("查询[扣吨计算单]完成: {}", TimeFormat.secondTime());
            return JsonResult.ok(tonnageEntity);
        }
        return JsonResult.fail(400, "请输入正确的采购接口服务");
    }


}
